/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package service;

import java.util.Date;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.TemporalType;

/**
 *
 * @author chapmand
 */
public abstract class AbstractFacade<T> {
    private Class<T> entityClass;

    public AbstractFacade(Class<T> entityClass) {
        this.entityClass = entityClass;
    }

    protected abstract EntityManager getEntityManager();
    
    public T create(T entity){
        getEntityManager().persist(entity);
        getEntityManager().flush();
        getEntityManager().refresh(entity);
        return entity;
    }

    public void edit(T entity) {
        getEntityManager().merge(entity);
        getEntityManager().getEntityManagerFactory().getCache().evictAll();
    }

    public void remove(T entity) {
        getEntityManager().remove(getEntityManager().merge(entity));
        getEntityManager().getEntityManagerFactory().getCache().evictAll();
    }

    public T find(Object id) {
        return getEntityManager().find(entityClass, id);
    }

    public List<T> findAll() {
        javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
        cq.select(cq.from(entityClass));
        return getEntityManager().createQuery(cq).getResultList();
    }

    public List<T> findRange(int[] range) {
        javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
        cq.select(cq.from(entityClass));
        javax.persistence.Query q = getEntityManager().createQuery(cq);
        q.setMaxResults(range[1] - range[0]);
        q.setFirstResult(range[0]);
        return q.getResultList();
    }

    public int count() {
        javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
        javax.persistence.criteria.Root<T> rt = cq.from(entityClass);
        cq.select(getEntityManager().getCriteriaBuilder().count(rt));
        javax.persistence.Query q = getEntityManager().createQuery(cq);
        return ((Long) q.getSingleResult()).intValue();
    }
    
    //Named Query Functions
    public List<T> runNamedQuery(String str){
        return getEntityManager().createNamedQuery(str).getResultList();
    }
    
    public List<T> runNamedQueryWithID(String str, int id){
        return getEntityManager().createNamedQuery(str)
                .setParameter(1, id).getResultList();
    }
    
    public List<T> runNamedQueryWithTwoID(String str, int id1, int id2){
        return getEntityManager().createNamedQuery(str)
                .setParameter(1, id1).setParameter(2, id2)
                .getResultList();
    }

    public List<T> findNamedQueryRange(int[] range, String str, int id) {
        return getEntityManager().createNamedQuery(str)
                .setParameter("cId", id).setMaxResults(range[1] - range[0])
                .setFirstResult(range[0]).getResultList();
    }
    
    public int namedQueryCount(String str, int id, String idStr) {
        List<T> list = getEntityManager().createNamedQuery(str).setParameter(idStr, id).getResultList();
        return list.size();
    }

    //Client Side custom query functions.
    public List<T> runQuery(String str) {
        return getEntityManager().createQuery(str).getResultList();
    }

    public List<T> runBetweenDateQuery(String str, Date startDate, Date endDate) {
        return getEntityManager().createQuery(str)
                .setParameter(1, startDate, TemporalType.TIMESTAMP)
                .setParameter(2, endDate, TemporalType.TIMESTAMP)
                .getResultList();
    }
    
}
